package org.ellisande.grid.tags;

import java.util.EnumSet;

import javax.servlet.jsp.JspWriter;

import org.ellisande.grid.tags.support.TagBuilder;
import org.ellisande.tld.annotations.Attribute;
import org.ellisande.tld.annotations.Description;
import org.ellisande.tld.annotations.Info;
import org.ellisande.tld.annotations.Tag;

@Tag
@Info("Defines navigation list item links. This tag expects to be in the body of the navList tag.")
public class NavListItemTag extends AbstractTag {

	private static final long serialVersionUID = -3267102435475190973L;

	protected static enum Type {
		LINK(""), HEADER(""),DIVIDER("");

		private String type;

		private Type(String type) {
			this.type = type;
		}

		public String toString() {
			return type;
		}
	}

	@Attribute
	@Description("Defines the type of the nav list item. Allowed values: link | header | divider. Default: link.")
	private Type type = Type.LINK;

	@Attribute
	@Description("Defines the URL that should be redirected to when this link is clicked. Only applicable if the type is \"link\".")
	private String href = "#";

	@Attribute
	@Description("Defines whether or not a nav list item should be selected. Only applicable if the type is \"link\".")
	private boolean active = false;
	
	@Attribute
	@Description("CSS class which defines the icon to place in front of the list item.")
	private String iconClass = "";

	private static final String ACTIVE_CLASS = "active";
	private static final String HEADER_CLASS = "nav-header";

	@Override
	public void startTag() throws Exception {
		TagBuilder listItem = createTagBuilder("li");
		TagBuilder anchor = createTagBuilder("a");
		TagBuilder icon = createTagBuilder("i");
		icon.addStyle(iconClass);
		
		if (active)
			listItem.addStyle(ACTIVE_CLASS);
		if (Type.HEADER.equals(type))
			listItem.addStyle(HEADER_CLASS);
		if(Type.DIVIDER.equals(type))
			listItem.addStyle("divider");
		anchor.addAttribute("href", href);

		JspWriter out = pageContext.getOut();
		out.println(listItem.compileStartTag());
		if (Type.LINK.equals(type))
			out.println(anchor.compileStartTag());
		if(!iconClass.isEmpty())
			out.println(icon.compileStartTag() + icon.compileEndTag());
	}

	@Override
	public void endTag() throws Exception {
		TagBuilder listItem = createTagBuilder("li");
		TagBuilder anchor = createTagBuilder("a");

		JspWriter out = pageContext.getOut();
		if (Type.LINK.equals(type))
			out.println(anchor.compileEndTag());
		out.println(listItem.compileEndTag());
	}

	public void setHref(String href) {
		this.href = href;
	}

	public void setActive(String active) {
		this.active = Boolean.parseBoolean(active);
	}

	public void setType(String link) {
		for (Type type : EnumSet.allOf(Type.class)) {
			if (type.name().equalsIgnoreCase(link)) {
				this.type = type;
				return;
			}
		}
	}

	@Override
	public void release() {
		super.release();
		href = "#";
		active = false;
	}
	
	public void setIconClass(String iconClass){
		this.iconClass = iconClass;
	}

}
